import { createRouter, createWebHistory, type RouteLocationNormalized } from 'vue-router'

//import NProgress from 'nprogress';

const router = createRouter({
    history: createWebHistory('c12100'),
    routes: [
        {
            path: '',
            name: 'view_home',
            component: () => import('@/views/home/view_home.vue'),
            redirect(to) {
                const R = window.sessionStorage.getItem('currentRoute') || `{"name": "dashboard"}`
                return JSON.parse(R)
            },
            children: [
                {
                    path: 'dashboard',
                    name: 'dashboard',
                    component: () => import('@/views/home/dashboard/view_dashboard.vue')
                },
                {
                    path: 'global-setting',
                    name: 'globalsetting',
                    component: () => import('@/views/home/global/view_globalSetting.vue')
                },
                {
                    path: 'folder',
                    name: 'folder',
                    component: () => import('@/views/home/folder/view_folder.vue')
                },
                {
                    path: 'log',
                    name: 'log',
                    component: () => import('@/views/home/log/view_log.vue')
                },
                {
                    path: 'user',
                    name: 'user',
                    children: [
                        {
                            path: 'global-user',
                            name: 'globaluser',

                            component: () => import('@/views/home/user/global/view_userGlobal.vue')
                        },
                        {
                            path: 'local',
                            name: 'localuser',
                            component: () => import('@/views/home/user/local/view_localUser.vue')
                        },
                        {
                            path: 'virtual',
                            name: 'virtualuser',
                            component: () => import('@/views/home/user/virtual/view_virtualUser.vue')
                        },
                        {
                            path: 'anonymous',
                            name: 'anonymous',
                            component: () => import('@/views/home/user/anonymous/view_anonymous.vue')
                        }
                    ]
                },
                {
                    path: 'group',
                    name: 'group',
                    component: () => import('@/views/home/group/view_group.vue')
                },
                {
                    path: 'config',
                    name: 'config',
                    component: () => import('@/views/home/config/view_config.vue')
                }
            ]
        },
        {
            path: '/test',
            name: 'test',
            component: () => import('@/views/test/test.vue')
        }
    ]
})

/*router.beforeEach(async (to, from, next) => {
    //NProgress.start()
    next()
})*/

// 路由后置守卫
router.afterEach((to: RouteLocationNormalized, _from: RouteLocationNormalized, _next) => {
    //NProgress.done()
    if (to.name == 'view_home') {
        return
    }
    sessionStorage.setItem(
        'currentRoute',
        JSON.stringify({
            path: to.path,
            query: to.query,
            params: to.params,
            hash: to.hash
        })
    )
})
export default router
